package com.java.service.impl;

import com.java.entity.QuestionOption;
import com.java.entity.QuestionOptionExample;
import com.java.mapper.QuestionOptionMapper;
import com.java.service.QuestionOptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class QuestionOptionServiceImpl implements QuestionOptionService {

    @Autowired
    private QuestionOptionMapper questionOptionMapper;

    @Override
    public int insert(QuestionOption questionOption) {
        return questionOptionMapper.insertSelective(questionOption);
    }

    @Override
    public int delete(Integer questionOptionId) {
        return questionOptionMapper.deleteByPrimaryKey(questionOptionId);
    }

    @Override
    public int update(QuestionOption questionOption) {
        return questionOptionMapper.updateByPrimaryKeySelective(questionOption);
    }

    @Override
    public QuestionOption selectOne(Integer questionOptionId) {
        QuestionOption questionOption = questionOptionMapper.selectByPrimaryKey(questionOptionId);
        if (questionOption == null) {
            throw new RuntimeException("questionOption is null, questionOptionId=" + questionOptionId);
        }
        return questionOption;
    }

    @Override
    public List<QuestionOption> selectSomeByQuestionId(Integer questionId) {
        QuestionOptionExample example = new QuestionOptionExample();
        example.or().andQuestionIdEqualTo(questionId);
        example.setOrderByClause("question_option_index");
        return questionOptionMapper.selectByExample(example);
    }

    @Override
    public List<QuestionOption> selectAll() {
        return questionOptionMapper.selectByExample(null);
    }

}
